﻿using System;
using System.Linq;
using EzDesk.Core.Model;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.ServiceLocator.CoreClientService;
using EzDesk.DesktopClient.UtilityView.ViewModel;
using EzDesk.Generic.Collections;

using EzDesk.Generic;
using EzDesk.Generic.Collections;
using EzDesk.Generic.WPF;
using Microsoft.Practices.Unity;

namespace EzDesk.DesktopClient.ViewModel
{
    using ViewContract;
    using MVVMEnhance;

    public class SelfInfoViewModel : ViewModelWithSingleContainer<ISelfInfoView>, ISelfInfoViewModel
    {
        public string UserHeadIcon
        {
            get { return UserManagement.Instance.HeadIconUrl; }
        }

        public UserProfile UserProfileModel
        {
            get
            {
                return UserManagement.Instance.LogUser;
            }

        }

        public Dept DeptModel
        {
            get
            {
                return UserManagement.Instance.LogUserDept;
            }
        }

        public string _selectedTodoFeedId;

        public IFeedItemViewModel SelectedTodoFeed
        {
            get
            {
                if(TodoFeeds == null || TodoFeeds.Count == 0) return null;

                var todoFeed = TodoFeeds.FirstOrDefault(t => t.FeedModel.FeedID == UserManagement.Instance.CurrentTodoId);
                return todoFeed ?? TodoFeeds[0];
            }
            set
            {
                
                _selectedTodoFeedId = value.FeedModel.FeedID;
                RaisePropertyChanged("SelectedTodoFeed");

                //ICoreClientService.SetCurrentTodo(_selectedTodoFeedId);
            }
        }

        public RangeCollection<IFeedItemViewModel> TodoFeeds{ get; set; }

        public SelfInfoViewModel():base()
        {
            Init();
        }

        public void Init()
        {
            TodoFeeds = new RangeCollection<IFeedItemViewModel>();
            LoginEvent.Instance.Subscribe(this.OnUserLogin);
            FeedPushEvent.Instance.Subscribe(this.OnFeedPush);
        }

        private void OnFeedPush(FeedPushArgs obj)
        {
            obj.ToDoCollection.Foreach(ProcessPushFeed,TodoFeeds);            
        }

        private void OnUserLogin(LoginArgs obj)
        {
            RaisePropertyChanged("DeptModel");
            RaisePropertyChanged("UserHeadIcon");
            RaisePropertyChanged("UserProfileModel");

            InitData();
        }

        private void InitData()
        {
            var feedDataArgs = DataManagement.Instance.GetDataFromLocal();
            OnFeedPush(feedDataArgs);
        }

        private void ProcessPushFeed( Model.Feed todo, RangeCollection<IFeedItemViewModel> viewModelCollection)
        {
            var itemViewModel = viewModelCollection.FirstOrDefault(uvm => uvm.FeedModel.FeedID == todo.FeedID);
            if (itemViewModel != null)
            {
                if (itemViewModel.FeedModel.UpdateOn != todo.UpdateOn)
                {
                    viewModelCollection.Remove(itemViewModel);
                    var viewModel = this.Container.Resolve<ITodoItemViewModel>().SetModel(todo);
                    viewModelCollection.InsertItemAtFirst(viewModel);
                }
                if (itemViewModel.FeedModel.LastChangeOn != todo.LastChangeOn)
                {
                    itemViewModel.IsRead = todo.IsRead.Value;
                    itemViewModel.IsMark = todo.IsMark.Value;
                    if (!todo.IsAvailable.Value)
                    {
                        viewModelCollection.Remove(itemViewModel);
                    }
                }
            }
            else
            {
                if (todo.IsAvailable.Value)
                {
                    var viewModel = this.Container.Resolve<ITodoItemViewModel>().SetModel(todo);
                    viewModelCollection.InsertItemAtFirst(viewModel);
                }
            }
        }

    }
}